var pageNum=1,pageSize=4,pageTotal=0;
$(function(){
	loadAllNoticeMessage();
});

function loadAllNoticeMessage(){
	utilAjax("/notice_admin/queryAllNotice","pageNum="+pageNum+"&pageSize="+pageSize,"get",
			function(data){
		pageNum = data.pageNum;
		pageSize = data.pageSize;
		pageTotal = data.pages;
		parseAllNoticeMessage(data.list);
	});
}
function parseAllNoticeMessage(lists){
	var notCon = "<tr class='info text-info'><td>公告编号</td><td>公司标题</td>"+
						"<td>公告内容</td><td>公告状态</td>"+
						"<td>创建时间</td><td>操作</td></tr>";
	if(lists!=""){
		$.each(lists,function(i,n){
			var noticeBut = "";
			if(n.notice_state==1){noticeBut = "<input type='button' value='下线公告' class='btn btn-default' onclick=xiaXianNotice("+n.notice_id+",2)>"}
			if(n.notice_state==2){noticeBut = "<input type='button' value='上线公告' class='btn btn-default' onclick=xiaXianNotice("+n.notice_id+",1)>"}
			notCon+="<tr>" +
						"<td>"+n.notice_id+"</td>" +
						"<td>"+n.notice_title+"</td>" +
						"<td><textarea class='form-control' rows='3'>"+n.notice_content+"</textarea></td>" +
						"<td>"+(n.notice_state==1?"<font color='green'>以上线</font>":"<font color='red'>未上线</font>")+"</td>" +
						"<td>"+dateUtil(n.notice_create_time)+"</td>" +
						"<td><input type='button' value='修改' class='btn btn-default' onclick='showUpdateNotice("+n.notice_id+")'> "+noticeBut+"</td>"+
					"</tr>";
		});
		notCon+= "<tr><td colspan='6' align='center'>当前页:"+pageNum+"/总页数:"+pageTotal+"</td></tr>";
		notCon+= "<tr><td><input type='button' value='新添公告' onclick='showInsertNotice()' class='btn btn-default'></td><td colspan='5' align='center'>" +
				"<input type='button' value='首页' class='btn btn-default' onclick=changeNoticePage(this)> " +
				"<input type='button' value='上一页' class='btn btn-default' onclick=changeNoticePage(this)> " +
				"<input type='button' value='下一页' class='btn btn-default' onclick=changeNoticePage(this)> " +
				"<input type='button' value='末页' class='btn btn-default' onclick=changeNoticePage(this)> " +
			"</td></tr>";
	}else{
		notCon+= "<tr><td colspan='6' align='center'><font size='6' color='red'>暂无公告</font></td></tr>";
	}
	$("#noticeTable").html(notCon);
}

function showUpdateNotice(notice_id){
	//显示添加公告
	$("#updateNoticeDiv").show();
	$("#noticeTable").hide();
	utilAjax("/notice_admin/queryByNotice_id","notice_id="+notice_id,"post",
			function(data){
		$("#updateNotice_id").val(data.notice_id);
		$("#updateNotice_title").val(data.notice_title);
		$("#updateNotice_content").val(data.notice_content);
	});
}

function showInsertNotice(){
	//显示添加公告
	$("#insertNoticeDiv").show();
	$("#noticeTable").hide();
}

function xiaXianNotice(notice_id,notice_state){
	utilAjax("/notice_admin/removeNotice","notice_id="+notice_id+"&notice_state="+notice_state,"post",
			function(flag){
		if(flag){
			alert("操作成功");
			loadAllNoticeMessage();
		}else{
		}
	});
}


function changeNoticePage(op){
	var butCon = $(op).val();
	switch(butCon){
	case "首页":
		pageNum=1;
		loadAllNoticeMessage();
		break;
	case "上一页":
		if(pageNum>1){
			pageNum--;
			loadAllNoticeMessage();
		}	
		break;
	case "下一页":
		if(pageNum<pageTotal){
			pageNum++;
			loadAllNoticeMessage();
		}
		break;
	case "末页":
		pageNum=pageTotal;
		loadAllNoticeMessage();
		break;
	}
}

function backUp(){
	$("#noticeTable").show();
	$("#insertNoticeDiv").hide();
}

function utilAjax(url,data,type,success){
	$.ajax({
		url:url,
		data:data,
		dataType:'json',
		type:type,
		success:success,
		error:function(){
		}
	});
}


function dateUtil(times){
	var date = new Date(times);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
	Y = date.getFullYear() + '-';
	M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date
			.getMonth() + 1)
			+ '-';
	D = date.getDate() + ' ';
	var dateCon = Y + M + D;
	return dateCon;
}